package com.cloud.wms.ui.returned.presenter;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.inside.entity.TransferUpEntity;
import com.cloud.wms.ui.returned.contact.InquireContact;
import com.cloud.wms.ui.returned.entity.InvoiceEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.functions.Function;

/**
 * Created by FC on 2021/5/20.
 * Desc：
 */
public class InquirePresenter extends BaseRxPresenter<InquireContact.View> implements InquireContact.Presenter {
    public static final int CHECK_COMMODITY = 0x100;
    public static final int RETURN_ORDER_INQUIRE = 0x101;
    public static final int CONFIRM_BIND_TO_INVENTORY = 0x102;

    @Inject
    public InquirePresenter() {

    }

    /**
     * 检测商品
     *
     * @param barCode
     */
    @Override
    public void checkCommodity(String barCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("barCode", barCode);
        Observable.timer(500, TimeUnit.MILLISECONDS)
                .map(new Function<Long, TransferCommodityEntity>() {
                    @Override
                    public TransferCommodityEntity apply(Long aLong) throws Exception {
                        TransferCommodityEntity commodityEntity = new TransferCommodityEntity();
                        commodityEntity.setCommodityName("测试商品");
                        commodityEntity.setBarCode(barCode);
                        return commodityEntity;
                    }
                })
//        FineExApplication.component().http().returnCheckCommodity(params)
//                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<TransferCommodityEntity>(mView.provideContext()) {
                    @Override
                    public void onSuccess(TransferCommodityEntity commodityEntity) {
                        mView.onSuccess(MessageCreator.createMessage(commodityEntity, CHECK_COMMODITY));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 根据退货面单号查询发货单
     *
     * @param orderCode
     * @param phone
     * @param commodityIDList
     */
    @Override
    public void confirmSelect(String orderCode, String phone, List<String> commodityIDList) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        Observable.timer(500, TimeUnit.MILLISECONDS)
                .map(new Function<Long, List<InvoiceEntity>>() {
                    @Override
                    public List<InvoiceEntity> apply(Long aLong) throws Exception {
                        List<InvoiceEntity> entityList = new ArrayList<>();
                        for (int i = 0; i < 3; i++) {
                            InvoiceEntity entity = new InvoiceEntity();
                            entity.setInvoiceCode("FHD" + i);
                            entity.setShippingAddress("上海市-上海市-静安区 智力产业园二期十二号楼" + i);
                            entity.setDeliveryTime("5021-05-1" + i);
                            List<TransferCommodityEntity> commodityList = new ArrayList<>();
                            for (int j = 0; j < 3; j++) {
                                TransferCommodityEntity commodityEntity = new TransferCommodityEntity();
                                commodityEntity.setCommodityName("测试商品" + j);
                                commodityEntity.setBarCode("TM" + j);
                                commodityEntity.setAmount(10);
                                commodityList.add(commodityEntity);
                            }
                            entity.setCommodityList(commodityList);
                            entityList.add(entity);
                        }
                        return entityList;
                    }
                })
//        FineExApplication.component().http().returnOrderInquire(params)
//                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<List<InvoiceEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(List<InvoiceEntity> invoiceEntities) {
                        mView.onSuccess(MessageCreator.createMessage(invoiceEntities, RETURN_ORDER_INQUIRE));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 退货单号与面单号进行绑定，去清点
     *
     * @param returnOrderCode
     * @param invoiceID
     */
    @Override
    public void confirmToInventory(String returnOrderCode, String invoiceID) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        Observable.timer(500, TimeUnit.MILLISECONDS)
                .map(new Function<Long, Boolean>() {
                    @Override
                    public Boolean apply(Long aLong) throws Exception {
                        return true;
                    }
                })
//        FineExApplication.component().http().confirmToInventory(params)
//                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Boolean>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Boolean aBoolean) {
                        mView.onSuccess(MessageCreator.createMessage(aBoolean, CONFIRM_BIND_TO_INVENTORY));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
